package com.elong.android.specialhouse.utils;

import android.content.res.ColorStateList;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class CalendarUtils {
	public final static int COMPARETYPE_DAY = 0;
	public final static int COMPARETYPE_HOUR = 1;
	public final static int COMPARETYPE_MINUTE = 2;

	/*
	 * private final static int[] TIME_VALUES = { 24 * 60 * 60 * 1000, 60 * 60 *
	 * 1000, 60 * 1000 };
	 */

	/*
	 * public int compareDate(Calendar c1,Calendar c2,int type) { switch(type) {
	 * case COMPARETYPE_DAY: Calendar c = (Calendar)c1.clone();
	 * c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0);
	 * c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0);
	 * 
	 * break; } return 0; }
	 */

	public final static int compareDate(Calendar c1, Calendar c2) {
		int y1 = c1.get(Calendar.YEAR);
		int y2 = c2.get(Calendar.YEAR);
		int m1 = c1.get(Calendar.MONTH);
		int m2 = c2.get(Calendar.MONTH);
		int d1 = c1.get(Calendar.DAY_OF_MONTH);
		int d2 = c2.get(Calendar.DAY_OF_MONTH);

		if (y1 == y2) {
			if (m1 == m2) {
				return d1 - d2;
			} else {
				return m1 - m2;
			}
		} else {
			return y1 - y2;
		}
	}

	public static int getDaysBetween(Calendar d1, Calendar d2) {
		if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
			Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		if (d1.get(Calendar.YEAR) != y2) {
			d1 = (Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
		return days;
	}

	public static int countDaysBetween(Calendar d1, Calendar d2) {
		return (int) ((d1.getTimeInMillis() - d2.getTimeInMillis()) / (1000 * 3600 * 24));
	}

	/**
	 * 该方法不做参数验证了，要求参数是：2010-1-1或2010-01-01这样的格式
	 */
	public static long getDaysBetween(String startDate, String endDate) {
		String startArr[] = startDate.split("-");
		String endArr[] = endDate.split("-");

		Calendar startCalendar = CalendarUtils.getCalendarInstance();
		Calendar endCalendar = CalendarUtils.getCalendarInstance();

		startCalendar.set(Integer.valueOf(startArr[0]), Integer.valueOf(startArr[1]), Integer.valueOf(startArr[2]));
		endCalendar.set(Integer.valueOf(endArr[0]), Integer.valueOf(endArr[1]), Integer.valueOf(endArr[2]));

		return getDaysBetween(startCalendar, endCalendar);
	}

	/**
	 * 
	 * @Title: getCalendarInstance
	 * @Description: 返回日历对象(默认采用东八区区时)
	 * @param @return
	 * @return Calendar
	 * @throws
	 */
	public static final Calendar getCalendarInstance() {
		return Calendar.getInstance();
//		return Calendar.getInstance(TimeZone.getTimeZone(IConfig.DEFAULT_TIMEZONE));
	}

	/**
	 * 
	 * @Title: parseCalendar
	 * @Description: 解析日期
	 * @param @return
	 * @return Calendar
	 * @throws
	 */
	public static Calendar parseCalendar(String dateStr) {
		SimpleDateFormat dateFormat;
		if (dateStr.contains("-")) {
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		} else {
			dateFormat = new SimpleDateFormat("yyyyMMdd");
		}
		Calendar c = CalendarUtils.getCalendarInstance();
		try {
			Date date = dateFormat.parse(dateStr);
			c.setTime(date);
		} catch (ParseException e) {
			c = null;
			e.printStackTrace();
		}

		return c;
	}

	/**
	 * 格式化输出当前时间
	 * 
	 * @param formatStr
	 * @return
	 */
	public static String formatCurrentTime(String formatStr) {
		// 使用默认时区和语言环境获得一个日历
		Calendar cale = Calendar.getInstance();
		// 将Calendar类型转换成Date类型
		Date tasktime = cale.getTime();
		// 设置日期输出的格式
		SimpleDateFormat df = new SimpleDateFormat(formatStr);
		// 格式化输出
		return df.format(tasktime);
	}

	/**
	 * * 获取指定日期是星期几 参数为null时表示获取当前日期是星期几
	 * 
	 * @param date
	 * @return
	 */
	public static String getWeekOfDate(Date date) {
		String[] weekOfDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekOfDays[w];
	}
	/**
	 * 根据Calendar获取星期数据
	 * 
	 * @param departDateTime
	 * @return
	 */
	public static String getWeek(Calendar departDateTime) {
		int weekDay = departDateTime.get(Calendar.DAY_OF_WEEK);
		String week = "";
		switch (weekDay) {
		case Calendar.MONDAY:
			week = "周一";
			break;
		case Calendar.TUESDAY:
			week = "周二";
			break;
		case Calendar.WEDNESDAY:

			week = "周三";
			break;
		case Calendar.THURSDAY:

			week = "周四";
			break;
		case Calendar.FRIDAY:
			week = "周五";
			break;
		case Calendar.SATURDAY:
			week = "周六";
			break;
		case Calendar.SUNDAY:
			week = "周日";
			break;

		default:
			break;
		}
		return week;
	}
	public static StateListDrawable createBGSelector(int normal, int pressed) {
		final StateListDrawable drawable = new StateListDrawable();
		ColorDrawable cdNormal = new ColorDrawable(normal);
		ColorDrawable cdPressed = new ColorDrawable(pressed);
		drawable.addState(new int[]{android.R.attr.state_pressed,
				android.R.attr.state_enabled}, cdPressed);
		drawable.addState(new int[]{android.R.attr.state_selected,
				android.R.attr.state_enabled}, cdPressed);
		drawable.addState(new int[] { android.R.attr.state_enabled }, cdNormal);
		return drawable;
	}
	public static ColorStateList createTextSelector(int normal, int pressed) {
		final ColorStateList colorStateList = new ColorStateList(new int[][] {
				new int[] { android.R.attr.state_pressed,
						android.R.attr.state_enabled },
				new int[] { android.R.attr.state_selected,
						android.R.attr.state_enabled },
				new int[] { android.R.attr.state_enabled } }, new int[] {
				pressed, pressed, normal });
		return colorStateList;
	}
}
